package com.iwhalecloud.bss.kite.cucc.service.route.strategy.impl;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IOrderService;
import com.iwhalecloud.bss.kite.cucc.client.api.scene.ISceneCommitService;
import com.iwhalecloud.bss.kite.cucc.client.dto.inf.InfServContextDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.IKitePosServiceHandler;
import com.iwhalecloud.bss.kite.cucc.service.route.strategy.IInfServStrategy;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.ztesoft.bss.common.util.SpringUtil;

import java.util.Map;

/**
 * <Description> <br>
 * 调用外中心服务总线策略
 *
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月3日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.route.strategy.impl <br>
 */
public class InfServiceBusStrategy implements IInfServStrategy {

    /**
     * Description: <br>
     * 外部中心订单归集接口
     *
     * @return <br>
     */
    @Override
    public Map<String, Object> orderCenterPathVariable(InfServContextDTO context) {
        ISceneCommitService sceneCommitService = SpringUtil.getBean(ISceneCommitService.class);
        sceneCommitService.checkWlwRealName(context.getSceneInstId(), context.getCustDto());

        // 组装下单请求对象
        IKitePosServiceHandler posServiceHandler = SpringUtil.getBean(IKitePosServiceHandler.class);
        KitePosDTO posDto = posServiceHandler.deal(context);

        String requestParam = JSONObject.toJSON(posDto).toString();
        IInfServiceBus infServ = SpringUtil.getBean(IInfServiceBus.class);

        Map<String, Object> result = infServ.orderCenterPathVariable(requestParam);

        return result;
    }

    /**
     * Description: <br>
     * 获取工单信息
     *
     * @param orderId
     * @param flowId
     * @param taskId
     * @return <br>
     */
    @Override
    public Map<String, Object> getWorkOrderInfo(String orderId, String flowId, String taskId) {
        IOrderService infServ = SpringUtil.getBean(IOrderService.class);
        return infServ.getWorkOrderInfo(orderId, flowId, taskId);
    }

    /**
     * 订单更新接口
     *
     * @param posDto
     * @param custId
     * @param orderId
     * @return
     */
    @Override
    public Map<String, Object> orderCenterUpdatePiece(InfServContextDTO context) {
        // 1.组装下单请求对象
        IKitePosServiceHandler posServiceHandler = SpringUtil.getBean(IKitePosServiceHandler.class);
        KitePosDTO posDto = posServiceHandler.deal(context.getSceneInstId(), context.getStaffId(), context.getOrgId(),
            context.getCustDto(), false, false, null);

        String custId = context.getCustDto().getCustId();
        String orderId = SpringUtil.getBean(IQuerySceneInstService.class).getExtParameterValue(context.getSceneInstId(),
            "orderId");

        IInfServiceBus infServ = SpringUtil.getBean(IInfServiceBus.class);
        return infServ.orderCenterUpdatePiece(posDto, custId, orderId);
    }
}
